package com.mock.action.organisation;

import java.util.ArrayList;
import java.util.List;

import com.mock.action.AbstractAction;
import com.mock.dao.AccreditationDao;
import com.mock.dao.BusinessTypeDao;
import com.mock.dao.ContactDao;
import com.mock.dao.CountryDao;
import com.mock.dao.OrganisationDao;
import com.mock.dao.OrganisationSpecialismDao;
import com.mock.dao.ProgrammeDao;
import com.mock.dao.ServiceBarriersCapabilityDao;
import com.mock.dao.ServiceBenefitsCapabilityDao;
import com.mock.dao.ServiceDao;
import com.mock.dao.ServiceDisabilitiesCapabilityDao;
import com.mock.dao.ServiceEthnicityCapabilityDao;
import com.mock.dao.ServicePersonalCircumstancesCapabilityDao;
import com.mock.dao.impl.AccreditationDaoImpl;
import com.mock.dao.impl.BusinessTypeDaoImpl;
import com.mock.dao.impl.ContactDaoImpl;
import com.mock.dao.impl.CountryDaoImpl;
import com.mock.dao.impl.OrganisationDaoImpl;
import com.mock.dao.impl.OrganisationSpecialismDaoImpl;
import com.mock.dao.impl.ProgrammeDaoImpl;
import com.mock.dao.impl.ServiceBarriersCapabilityDaoImpl;
import com.mock.dao.impl.ServiceBenefitsCapabilityDaoImpl;
import com.mock.dao.impl.ServiceDaoImpl;
import com.mock.dao.impl.ServiceDisabilitiesCapabilityDaoImpl;
import com.mock.dao.impl.ServiceEthnicityCapabilityDaoImpl;
import com.mock.dao.impl.ServicePersonalCircumstancesCapabilityDaoImpl;
import com.mock.model.Accreditation;
import com.mock.model.BusinessType;
import com.mock.model.Contact;
import com.mock.model.Country;
import com.mock.model.Organisation;
import com.mock.model.OrganisationSpecialism;
import com.mock.model.Programme;
import com.mock.model.Service;
import com.mock.model.ServiceBarriersCapability;
import com.mock.model.ServiceBenefitsCapability;
import com.mock.model.ServiceDisabilitiesCapability;
import com.mock.model.ServiceEthnicityCapability;
import com.mock.model.ServicePersonalCircumstancesCapability;

public class EditOrganisationAction extends AbstractAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	public String execute() throws Exception {

		organisation = organisationDao.getById(organisationId);				
		organisationSpecialisms1 = organisationSpecialismDao.getOrganisationDetailsTwoByOrganisationGerneric(organisation);
		disabilitiesCapabilities1 = serviceDisabilitiesCapabilityDao.getOrganisationDetailsTwoByOrganisationGerneric(organisation);		
		barriersCapabilities1 = serviceBarriersCapabilityDao.getOrganisationDetailsTwoByOrganisationGerneric(organisation);
		benefitsCapabilities1 = serviceBenefitsCapabilityDao.getOrganisationDetailsTwoByOrganisationGerneric(organisation);
		servicePersonalCircumstancesCapabilities1 = servicePersonalCircumstancesCapabilityDao.getOrganisationDetailsTwoByOrganisationGerneric(organisation);
		ethnicityCapabilities1 = serviceEthnicityCapabilityDao.getOrganisationDetailsTwoByOrganisationGerneric(organisation);		
		accreditations1 = accreditationDao.getOrganisationDetailsTwoByOrganisationGerneric(organisation);
		programmes1 = programmeDao.getOrganisationDetailsTwoByOrganisationGerneric(organisation);
		services1 = serviceDao.getOrganisationDetailsTwoByOrganisationGerneric(organisation);
		
		generateData();
		return SUCCESS;
	}
	
	public String update() throws Exception {		
		Organisation organisation1 = organisationDao.getById(organisationId);
		organisation1.setOrganisationName(organisationName);
		organisation1.setOrganisationShortDescription(organisationShortDescription);
		Contact contact = contactDao.getById(leadContactId);
		organisation1.setLeadContact(contact);
		organisation1.setAddressLine1(addressLine1);
		organisation1.setAddressLine2(addressLine2);
		organisation1.setAddressLine3(addressLine3);
		organisation1.setPostcode(postcode);
		organisation1.setCity(city);
		organisation1.setCounty(county);
		Country country2 = countryDao.getById(country);
		organisation1.setCountry(country2);
		organisation1.setPreferredOrganisation(preferredOrganisation);
		organisation1.setExpressionOfInterest(expressionOfInterest);		
		BusinessType businessType1 = businessTypeDao.getById(businessId);
		organisation1.setBusinessType(businessType1);
		organisation1.setOrganisationFullDescription(organisationFullDescription);
		organisation1.setPhoneNumber(phoneNumber);
		organisation1.setFax(fax);
		organisation1.setEmail(email);
		organisation1.setWebAddress(webAddress);
		organisation1.setCharityNumber(charityNumber);
		organisation1.setCompanyNumber(companyNumber);
		
		List<OrganisationSpecialism> organisationSpecialisms2 = new ArrayList<OrganisationSpecialism>();
		for (Long id : organisationSpecialism) {
			organisationSpecialisms2.add(organisationSpecialismDao.getById(id));
		}
		organisation1.setOrganisationSpecialism(organisationSpecialisms2);
		
		List<ServiceDisabilitiesCapability> disabilitiesCapabilities2 = new ArrayList<ServiceDisabilitiesCapability>();
		for (Long id : disabilitiesCapability) {
			disabilitiesCapabilities2.add(serviceDisabilitiesCapabilityDao.getById(id));
		}
		organisation1.setServiceDisabilitiesCapability(disabilitiesCapabilities2);
		
		List<ServiceBarriersCapability> barriersCapabilities2 = new ArrayList<ServiceBarriersCapability>();
		for (Long id : barriersCapability) {
			barriersCapabilities2.add(serviceBarriersCapabilityDao.getById(id));
		}
		organisation1.setServiceBarriersCapability(barriersCapabilities2);
		
		List<ServiceBenefitsCapability> benefitsCapabilities2 = new ArrayList<ServiceBenefitsCapability>();
		for (Long id : benefitsCapability) {
			benefitsCapabilities2.add(serviceBenefitsCapabilityDao.getById(id));
		}
		organisation1.setServiceBenefitsCapability(benefitsCapabilities2);
		
		List<ServicePersonalCircumstancesCapability> servicePersonalCircumstancesCapabilities2 = new ArrayList<ServicePersonalCircumstancesCapability>();
		for (Long id : servicePersonalCircumstancesCapability) {
			servicePersonalCircumstancesCapabilities2.add(servicePersonalCircumstancesCapabilityDao.getById(id));
		}
		organisation1.setServicePersonalCircumstancesCapability(servicePersonalCircumstancesCapabilities2);
		
		List<ServiceEthnicityCapability> ethnicityCapabilities2 = new ArrayList<ServiceEthnicityCapability>();
		for (Long id : ethnicityCapability) {
			ethnicityCapabilities2.add(serviceEthnicityCapabilityDao.getById(id));
		}
		organisation1.setServiceEthnicityCapability(ethnicityCapabilities2);
		
		List<Accreditation> accreditations2 = new ArrayList<Accreditation>();
		for (Long id : accreditation) {
			accreditations2.add(accreditationDao.getById(id));
		}
		organisation1.setAccreditation(accreditations2);
		
		List<Programme> programmes2 = new ArrayList<Programme>();
		List<Service> services2 = new ArrayList<Service>();
		
		if (expressionOfInterest == true) {
			programmes2 = new ArrayList<Programme>();
			for (Long id : programme) {
				programmes2.add(programmeDao.getProgrammeById(id));
			}
			organisation1.setProgrammes(programmes2);
			
			services2 = new ArrayList<Service>();
			for (Long id : service) {
				services2.add(serviceDao.getById(id));
			}
			organisation1.setServices(services2);			
		} else {
			programmes2 = new ArrayList<Programme>();
			organisation1.setProgrammes(programmes2);
			services2 = new ArrayList<Service>();
			organisation1.setServices(services2);
		}
		
		organisationDao.update(organisation1);
		return SUCCESS;
	}
	
	public String markActive() throws Exception {
		organisationId = Long.valueOf(getRequest().getParameter("organisationId"));
		OrganisationDao organisationDao = new OrganisationDaoImpl();
		Organisation organisation = organisationDao.getById(organisationId);
		organisation.setActive(false);
		organisationDao.update(organisation);
		return SUCCESS;
	}

	public void generateData() {		
		countries = countryDao.getAllCountry();
		programmes = programmeDao.getActiveProgramme();
		services = serviceDao.getAllServices();
		organisationSpecialisms = organisationSpecialismDao.getAllOrganisationSpecialism();
		disabilitiesCapabilities = serviceDisabilitiesCapabilityDao.getAllServiceDisabilitiesCapability();
		barriersCapabilities = serviceBarriersCapabilityDao.getAllServiceBarriersCapability();
		benefitsCapabilities = serviceBenefitsCapabilityDao.getAllServiceBenefitsCapability();
		servicePersonalCircumstancesCapabilities = servicePersonalCircumstancesCapabilityDao.getAllServicePersonalCircumstancesCapability();
		ethnicityCapabilities = serviceEthnicityCapabilityDao.getAllServiceEthnicityCapability();
		accreditations = accreditationDao.getAllAccreditation();
	}
	
	private ContactDao contactDao = new ContactDaoImpl();
	private BusinessTypeDao businessTypeDao = new BusinessTypeDaoImpl();
	
	private List<Country> countries = new ArrayList<Country>();		
	private CountryDao countryDao = new CountryDaoImpl();
	
	private List<OrganisationSpecialism> organisationSpecialisms = new ArrayList<OrganisationSpecialism>();
	private List<ServiceDisabilitiesCapability> disabilitiesCapabilities = new ArrayList<ServiceDisabilitiesCapability>();
	private List<ServiceBarriersCapability> barriersCapabilities = new ArrayList<ServiceBarriersCapability>();
	private List<ServiceBenefitsCapability> benefitsCapabilities = new ArrayList<ServiceBenefitsCapability>();
	private List<ServicePersonalCircumstancesCapability> servicePersonalCircumstancesCapabilities = new ArrayList<ServicePersonalCircumstancesCapability>();
	private List<ServiceEthnicityCapability> ethnicityCapabilities = new ArrayList<ServiceEthnicityCapability>();
	private List<Accreditation> accreditations = new ArrayList<Accreditation>();
	
	private List<Long> organisationSpecialisms1 = new ArrayList<Long>();
	private List<Long> disabilitiesCapabilities1 = new ArrayList<Long>();
	private List<Long> barriersCapabilities1 = new ArrayList<Long>();
	private List<Long> benefitsCapabilities1 = new ArrayList<Long>();
	private List<Long> servicePersonalCircumstancesCapabilities1 = new ArrayList<Long>();
	private List<Long> ethnicityCapabilities1 = new ArrayList<Long>();
	private List<Long> accreditations1 = new ArrayList<Long>();
	
	private OrganisationSpecialismDao organisationSpecialismDao = new OrganisationSpecialismDaoImpl();
	private ServiceDisabilitiesCapabilityDao serviceDisabilitiesCapabilityDao = new ServiceDisabilitiesCapabilityDaoImpl();
	private ServiceBarriersCapabilityDao serviceBarriersCapabilityDao = new ServiceBarriersCapabilityDaoImpl();
	private ServiceBenefitsCapabilityDao serviceBenefitsCapabilityDao = new ServiceBenefitsCapabilityDaoImpl();
	private ServicePersonalCircumstancesCapabilityDao servicePersonalCircumstancesCapabilityDao = new ServicePersonalCircumstancesCapabilityDaoImpl();
	private ServiceEthnicityCapabilityDao serviceEthnicityCapabilityDao = new ServiceEthnicityCapabilityDaoImpl();
	private AccreditationDao accreditationDao = new AccreditationDaoImpl();
	
	private List<Programme> programmes = new ArrayList<Programme>();
	private List<Service> services = new ArrayList<Service>();
	
	private List<Long> programmes1 = new ArrayList<Long>();
	private List<Long> services1 = new ArrayList<Long>();
	
	private ProgrammeDao programmeDao = new ProgrammeDaoImpl();
	private ServiceDao serviceDao = new ServiceDaoImpl();
	
	private Organisation organisation;
	private OrganisationDao organisationDao = new OrganisationDaoImpl();	
	private BusinessType businessType;		
	private long organisationId = -1L;
	
	private String organisationName;
	private String organisationShortDescription;
	private Long leadContactId;
	private String leadContactName;
	private String addressLine1;
	private String addressLine2;
	private String addressLine3;
	private String postcode;
	private String city;
	private String county;
	private Long country;
	private boolean preferredOrganisation;
	private boolean expressionOfInterest;
	private Long businessId;
	private String businessName;
	private String sICCodeField;
	private String organisationFullDescription;
	private String phoneNumber;
	private String fax;
	private String email;
	private String webAddress;
	private String charityNumber;
	private String companyNumber;

	private List<Long> organisationSpecialism;
	private List<Long> disabilitiesCapability;
	private List<Long> barriersCapability;
	private List<Long> benefitsCapability;
	private List<Long> servicePersonalCircumstancesCapability;
	private List<Long> ethnicityCapability;
	private List<Long> accreditation;
		
	private List<Long> programme;
	private List<Long> service;
	
	public List<Country> getCountries() {
		return countries;
	}

	public void setCountries(List<Country> countries) {
		this.countries = countries;
	}

	public List<OrganisationSpecialism> getOrganisationSpecialisms() {
		return organisationSpecialisms;
	}

	public void setOrganisationSpecialisms(
			List<OrganisationSpecialism> organisationSpecialisms) {
		this.organisationSpecialisms = organisationSpecialisms;
	}

	public List<ServiceDisabilitiesCapability> getDisabilitiesCapabilities() {
		return disabilitiesCapabilities;
	}

	public void setDisabilitiesCapabilities(
			List<ServiceDisabilitiesCapability> disabilitiesCapabilities) {
		this.disabilitiesCapabilities = disabilitiesCapabilities;
	}

	public List<ServiceBarriersCapability> getBarriersCapabilities() {
		return barriersCapabilities;
	}

	public void setBarriersCapabilities(
			List<ServiceBarriersCapability> barriersCapabilities) {
		this.barriersCapabilities = barriersCapabilities;
	}

	public List<ServiceBenefitsCapability> getBenefitsCapabilities() {
		return benefitsCapabilities;
	}

	public void setBenefitsCapabilities(
			List<ServiceBenefitsCapability> benefitsCapabilities) {
		this.benefitsCapabilities = benefitsCapabilities;
	}

	public List<ServicePersonalCircumstancesCapability> getServicePersonalCircumstancesCapabilities() {
		return servicePersonalCircumstancesCapabilities;
	}

	public void setServicePersonalCircumstancesCapabilities(
			List<ServicePersonalCircumstancesCapability> servicePersonalCircumstancesCapabilities) {
		this.servicePersonalCircumstancesCapabilities = servicePersonalCircumstancesCapabilities;
	}

	public List<ServiceEthnicityCapability> getEthnicityCapabilities() {
		return ethnicityCapabilities;
	}

	public void setEthnicityCapabilities(
			List<ServiceEthnicityCapability> ethnicityCapabilities) {
		this.ethnicityCapabilities = ethnicityCapabilities;
	}

	public List<Accreditation> getAccreditations() {
		return accreditations;
	}

	public void setAccreditations(List<Accreditation> accreditations) {
		this.accreditations = accreditations;
	}

	public List<Long> getOrganisationSpecialisms1() {
		return organisationSpecialisms1;
	}

	public void setOrganisationSpecialisms1(
			List<Long> organisationSpecialisms1) {
		this.organisationSpecialisms1 = organisationSpecialisms1;
	}

	public List<Long> getDisabilitiesCapabilities1() {
		return disabilitiesCapabilities1;
	}

	public void setDisabilitiesCapabilities1(
			List<Long> disabilitiesCapabilities1) {
		this.disabilitiesCapabilities1 = disabilitiesCapabilities1;
	}

	public List<Long> getBarriersCapabilities1() {
		return barriersCapabilities1;
	}

	public void setBarriersCapabilities1(
			List<Long> barriersCapabilities1) {
		this.barriersCapabilities1 = barriersCapabilities1;
	}

	public List<Long> getBenefitsCapabilities1() {
		return benefitsCapabilities1;
	}

	public void setBenefitsCapabilities1(
			List<Long> benefitsCapabilities1) {
		this.benefitsCapabilities1 = benefitsCapabilities1;
	}

	public List<Long> getServicePersonalCircumstancesCapabilities1() {
		return servicePersonalCircumstancesCapabilities1;
	}

	public void setServicePersonalCircumstancesCapabilities1(
			List<Long> servicePersonalCircumstancesCapabilities1) {
		this.servicePersonalCircumstancesCapabilities1 = servicePersonalCircumstancesCapabilities1;
	}

	public List<Long> getEthnicityCapabilities1() {
		return ethnicityCapabilities1;
	}

	public void setEthnicityCapabilities1(
			List<Long> ethnicityCapabilities1) {
		this.ethnicityCapabilities1 = ethnicityCapabilities1;
	}

	public List<Long> getAccreditations1() {
		return accreditations1;
	}

	public void setAccreditations1(List<Long> accreditations1) {
		this.accreditations1 = accreditations1;
	}

	public List<Programme> getProgrammes() {
		return programmes;
	}

	public void setProgrammes(List<Programme> programmes) {
		this.programmes = programmes;
	}

	public List<Service> getServices() {
		return services;
	}

	public void setServices(List<Service> services) {
		this.services = services;
	}

	public List<Long> getProgrammes1() {
		return programmes1;
	}

	public void setProgrammes1(List<Long> programmes1) {
		this.programmes1 = programmes1;
	}

	public List<Long> getServices1() {
		return services1;
	}

	public void setServices1(List<Long> services1) {
		this.services1 = services1;
	}

	public Organisation getOrganisation() {
		return organisation;
	}

	public void setOrganisation(Organisation organisation) {
		this.organisation = organisation;
	}

	public BusinessType getBusinessType() {
		return businessType;
	}

	public void setBusinessType(BusinessType businessType) {
		this.businessType = businessType;
	}

	public long getOrganisationId() {
		return organisationId;
	}

	public void setOrganisationId(long organisationId) {
		this.organisationId = organisationId;
	}

	public String getOrganisationName() {
		return organisationName;
	}

	public void setOrganisationName(String organisationName) {
		this.organisationName = organisationName;
	}

	public String getOrganisationShortDescription() {
		return organisationShortDescription;
	}

	public void setOrganisationShortDescription(String organisationShortDescription) {
		this.organisationShortDescription = organisationShortDescription;
	}

	public Long getLeadContactId() {
		return leadContactId;
	}

	public void setLeadContactId(Long leadContactId) {
		this.leadContactId = leadContactId;
	}

	public String getLeadContactName() {
		return leadContactName;
	}

	public void setLeadContactName(String leadContactName) {
		this.leadContactName = leadContactName;
	}

	public String getAddressLine1() {
		return addressLine1;
	}

	public void setAddressLine1(String addressLine1) {
		this.addressLine1 = addressLine1;
	}

	public String getAddressLine2() {
		return addressLine2;
	}

	public void setAddressLine2(String addressLine2) {
		this.addressLine2 = addressLine2;
	}

	public String getAddressLine3() {
		return addressLine3;
	}

	public void setAddressLine3(String addressLine3) {
		this.addressLine3 = addressLine3;
	}

	public String getPostcode() {
		return postcode;
	}

	public void setPostcode(String postcode) {
		this.postcode = postcode;
	}

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public String getCounty() {
		return county;
	}

	public void setCounty(String county) {
		this.county = county;
	}

	public Long getCountry() {
		return country;
	}

	public void setCountry(Long country) {
		this.country = country;
	}

	public boolean isPreferredOrganisation() {
		return preferredOrganisation;
	}

	public void setPreferredOrganisation(boolean preferredOrganisation) {
		this.preferredOrganisation = preferredOrganisation;
	}

	public boolean isExpressionOfInterest() {
		return expressionOfInterest;
	}

	public void setExpressionOfInterest(boolean expressionOfInterest) {
		this.expressionOfInterest = expressionOfInterest;
	}

	public Long getBusinessId() {
		return businessId;
	}

	public void setBusinessId(Long businessId) {
		this.businessId = businessId;
	}

	public String getBusinessName() {
		return businessName;
	}

	public void setBusinessName(String businessName) {
		this.businessName = businessName;
	}

	public String getsICCodeField() {
		return sICCodeField;
	}

	public void setsICCodeField(String sICCodeField) {
		this.sICCodeField = sICCodeField;
	}

	public String getOrganisationFullDescription() {
		return organisationFullDescription;
	}

	public void setOrganisationFullDescription(String organisationFullDescription) {
		this.organisationFullDescription = organisationFullDescription;
	}

	public String getPhoneNumber() {
		return phoneNumber;
	}

	public void setPhoneNumber(String phoneNumber) {
		this.phoneNumber = phoneNumber;
	}

	public String getFax() {
		return fax;
	}

	public void setFax(String fax) {
		this.fax = fax;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getWebAddress() {
		return webAddress;
	}

	public void setWebAddress(String webAddress) {
		this.webAddress = webAddress;
	}

	public String getCharityNumber() {
		return charityNumber;
	}

	public void setCharityNumber(String charityNumber) {
		this.charityNumber = charityNumber;
	}

	public String getCompanyNumber() {
		return companyNumber;
	}

	public void setCompanyNumber(String companyNumber) {
		this.companyNumber = companyNumber;
	}

	public List<Long> getOrganisationSpecialism() {
		return organisationSpecialism;
	}

	public void setOrganisationSpecialism(List<Long> organisationSpecialism) {
		this.organisationSpecialism = organisationSpecialism;
	}

	public List<Long> getDisabilitiesCapability() {
		return disabilitiesCapability;
	}

	public void setDisabilitiesCapability(List<Long> disabilitiesCapability) {
		this.disabilitiesCapability = disabilitiesCapability;
	}

	public List<Long> getBarriersCapability() {
		return barriersCapability;
	}

	public void setBarriersCapability(List<Long> barriersCapability) {
		this.barriersCapability = barriersCapability;
	}

	public List<Long> getBenefitsCapability() {
		return benefitsCapability;
	}

	public void setBenefitsCapability(List<Long> benefitsCapability) {
		this.benefitsCapability = benefitsCapability;
	}

	public List<Long> getServicePersonalCircumstancesCapability() {
		return servicePersonalCircumstancesCapability;
	}

	public void setServicePersonalCircumstancesCapability(
			List<Long> servicePersonalCircumstancesCapability) {
		this.servicePersonalCircumstancesCapability = servicePersonalCircumstancesCapability;
	}

	public List<Long> getEthnicityCapability() {
		return ethnicityCapability;
	}

	public void setEthnicityCapability(List<Long> ethnicityCapability) {
		this.ethnicityCapability = ethnicityCapability;
	}

	public List<Long> getAccreditation() {
		return accreditation;
	}

	public void setAccreditation(List<Long> accreditation) {
		this.accreditation = accreditation;
	}

	public List<Long> getProgramme() {
		return programme;
	}

	public void setProgramme(List<Long> programme) {
		this.programme = programme;
	}

	public List<Long> getService() {
		return service;
	}

	public void setService(List<Long> service) {
		this.service = service;
	}
	
}
